<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
    <script src="../node_modules/vue/dist/vue.js"></script>
  </head>

  <body>
    <div id="app">
      <cpn @item-click="childClick"></cpn>
    </div>
    <template id="cpn">
      <div>
        <button v-for="item in categories" @click="clickHandler(item.id)">
          {{item.name}}
        </button>
      </div>
    </template>
    <script>
      const cpn = {
        template: "#cpn",
        data() {
          return {
            categories: [
              { id: "aaa", name: "手机数码" },
              { id: "bbb", name: "热门推荐" },
              { id: "ccc", name: "生鲜水果" },
            ],
          };
        },
        methods: {
          clickHandler(id) {
            // console.log(id, "被点击了");
            this.$emit("item-click", id);
          },
        },
      };
      // 子组件发生时间，通过时间把数据传递给父组件
      var vm = new Vue({
        el: "#app",
        data: {
          message: "HELLO,WORLD",
        },
        methods: {
          childClick(id) {
            console.log("父组件接受到子组件的参数：", id);
          },
        },
        components: {
          cpn,
        },
      });
    </script>
  </body>
</html>
